package com.leyou.item.controller;

import com.leyou.item.interfaces.pojo.Category;
import com.leyou.item.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类参数列表
 * @version 1.0.0
 * @Author yong.Mr
 * @data 2019-8-9
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/category")
public class CategoryController {

    /**
     * 注入CategoryService
     */
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询商品目录参数列表
     * @param pid 点击目录时的id
     * @return ResponseEntity<List<Category>>
     */
    @GetMapping("/list")
    public ResponseEntity<List<Category>> queryCategoryByPid(@RequestParam(name = "pid",required = false,defaultValue = "0") Long pid){
        try {
            // 判断pid是否合法 pid.longValue()转换为基本数据类型
            if(pid == null || pid.longValue() < 0 ){
                // 相应400 , 参数不合法
                // return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();  ==
                return ResponseEntity.badRequest().build();
            }
            // 调用service业务
            List<Category> categories = this.categoryService.queryCategoryByPid(pid);
        /* 判断categories是否为空,是否查询得到
           -----> categories.size() == 0 || categories == null这种写法是错误的,如果categories == null则categories.size() == 0报空指针异常
           正确写法categories == null || categories.size() == 0 ok */
            if(CollectionUtils.isEmpty(categories)){
                // 没有查询到数据 相应404, 资源服务器未找到
                // return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); ==
                return ResponseEntity.notFound().build();
            }
            return ResponseEntity.ok(categories);
        }catch (Exception e){
            e.printStackTrace();
        }
        // 服务器异常, 500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 根据品牌Id查询商品分类
     * @param bid 品牌id
     * @return List<Category>
     */
    @RequestMapping("bid/{bid}")
    public ResponseEntity<List<Category>> queryByBrandId(@PathVariable("bid") Long bid) {
        List<Category> categories = categoryService.queryByBrandId(bid);
        // collection == null || collection.isEmpty();
        if(CollectionUtils.isEmpty(categories)){
          //  ResponseEntity responseEntity = new ResponseEntity<>(HttpStatus.NOT_FOUND);
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(categories);
    }

    /**
     *
     * @param ids
     * @return
     */
    @GetMapping
    public ResponseEntity<List<String>> queryNamesByIds(@RequestParam("ids")List<Long> ids){
        System.out.println("数据是否传过来了：" + ids.toString());
        List<String> names = this.categoryService.queryNamesByIds(ids);
        if (CollectionUtils.isEmpty(names)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(names);
    }

    /**
     * 根据3级分类id，查询1~3级的分类
     * @param id
     * @return
     */
    @GetMapping("all/level")
    public ResponseEntity<List<Category>> queryAllByCid3(@RequestParam("id") Long id){
        List<Category> list = this.categoryService.queryAllByCid3(id);
        if (list == null || list.size() < 1) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(list);
    }

}
